package com.kabigon.multiverse.component;

import com.kabigon.multiverse.common.valid.EnumString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.List;

/**
 * 枚举字符串验证器
 *
 * @author xu.biao
 * @version v1.0
 * @Package com.kabigon.multiverse.component
 * @date 2022/02/21
 */
@Slf4j
@Component
public class EnumStringValidator implements ConstraintValidator<EnumString, String> {

    private List<String> enumStringList;

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (value == null) {
            return true;
        }
        return enumStringList.contains(value);
    }

    @Override
    public void initialize(EnumString constraintAnnotation) {
        log.info("EnumStringValidator initialize");
        enumStringList = Arrays.asList(constraintAnnotation.value());
    }
}
